
<template>
  <!--  导航栏-->
  <el-row>
    <el-col :span="24">
      <el-breadcrumb separator=">">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>质量管理</el-breadcrumb-item>
        <el-breadcrumb-item>出货检验</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
  </el-row>
<!--  搜索栏-->
  <el-row style="margin-top: 20px">
    <el-col :span="24">
      <el-form :inline="true" :model="shipmentQuery">
        <el-form-item label="出货检验单编号">
          <el-input v-model="shipmentQuery.shipmentId" placeholder="请输入出货检验单编号"></el-input>
        </el-form-item>
        <el-form-item label="客户名称">
          <el-input v-model="shipmentQuery.shipmentClientName" placeholder="请输入客户名称"></el-input>
        </el-form-item>
        <el-form-item label="批次号">
          <el-input v-model="shipmentQuery.shipmentLotNumber" placeholder="请输入批次号"></el-input>
        </el-form-item>
        <el-form-item label="产品物料编码">
          <el-input v-model="shipmentQuery.shipmentItemId" placeholder="请输入产品物料编码"></el-input>
        </el-form-item>
        <el-form-item label="产品物料名称">
          <el-input v-model="shipmentQuery.shipmentItemName" placeholder="请输入产品物料名称"></el-input>
        </el-form-item>
        <el-form-item label="检测结果">
          <el-select v-model="shipmentQuery.materialOutcome" placeholder="请选择检测结果" style="width: 200px">
            <el-option label="全部" :value="0"></el-option>
            <el-option label="检验通过" :value="1"></el-option>
            <el-option label="检验不通过" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadShipments">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
<!--  工具栏-->
  <el-row style="margin-top: 20px">
    <el-col :span="24">
      <el-button type="primary" @click="addShipment" >新增</el-button>
      <el-button type="success">修改</el-button>
      <el-button type="danger" @click="batchDeleteShipment">删除</el-button>
    </el-col>
  </el-row>
<!--  内容栏-->
  <el-row style="margin-top: 20px">
    <el-col :span="24">
      <el-table :data="shipments" border @selection-change="selectShipment" >
        <el-table-column type="selection" width="55" ></el-table-column>
        <el-table-column width="130" align="center" prop="shipmentId" label="出货检验单编号"></el-table-column>
        <el-table-column width="130" align="center" prop="shipmentClientName" label="客户名称"></el-table-column>
        <el-table-column width="130" align="center" prop="shipmentLotNumber" label="批次号"></el-table-column>
        <el-table-column width="130" align="center" prop="shipmentItemId" label="产品物料编码"></el-table-column>
        <el-table-column width="130" align="center" prop="shipmentItemName" label="产品物料名称"></el-table-column>
        <el-table-column width="130" align="center" prop="shipmentItemSpecification" label="规格型号"></el-table-column>
        <el-table-column width="130" align="center" prop="shipmentItemUnit" label="单位"></el-table-column>
        <el-table-column width="130" align="center" prop="shipmentNumber" label="发货数量"></el-table-column>
        <el-table-column width="130" align="center" prop="materialNumber" label="检测数量"></el-table-column>
        <el-table-column width="130" align="center" prop="shipmentOutCount" label="不合格数"></el-table-column>
        <el-table-column width="130" align="center" prop="materialOutcome" label="检测结果">
          <template #default="scope">
            <el-tag v-if="scope.row.materialOutcome == 1" type="success">检测通过</el-tag>
            <el-tag v-else type="danger">检测不通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column width="130" align="center" prop="shipmentDate" label="出货日期"></el-table-column>
        <el-table-column width="130" align="center" prop="materialDate" label="检测日期"></el-table-column>
        <el-table-column width="130" align="center" prop="materialPerson" label="检测人员"></el-table-column>
        <el-table-column width="130" align="center" prop="shipmentStatus" label="单据状态">
          <template #default="scope">
            <el-tag v-if="scope.row.shipmentStatus==1" type="success">草稿</el-tag>
            <el-tag v-if="scope.row.shipmentStatus==2" type="info">已确认</el-tag>
          </template>
        </el-table-column>
        <el-table-column width="130" align="center" label="操作">
          <template #default="scope">
            <el-button type="text" @click="editShipment(scope.row.shipmentId)">修改</el-button>
            <el-button type="text" @click="deleteShipment(scope.row.shipmentId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>
<!--  分页栏-->
  <el-row style="margin-top: 15px">
    <el-col :span="24">
      <el-pagination @change="changePage" background layout="prev, pager, next" :total="total"/>
    </el-col>
  </el-row>
<!--  新增弹窗-->
  <el-dialog title="新增出货检验单" v-model="newShipmentDialogVisible">
    <el-form :model="newShipment" :inline="true" label-width="120px">
      <el-form-item label="检验单编号">
        <el-input v-model="newShipment.shipmentId" placeholder="请输入出货检验单编号" ></el-input>
      </el-form-item>
      <el-form-item label="检验单名称">
        <el-input v-model="newShipment.shipmentName" placeholder="请输入出货检验单名称"></el-input>
      </el-form-item>
      <el-form-item label="产品编号">
        <el-input v-model="newShipment.shipmentItemId" placeholder="请输入产品物料编码"></el-input>
      </el-form-item>
      <el-form-item label="产品名称">
        <el-input v-model="newShipment.shipmentItemName" placeholder="请输入产品物料名称"></el-input>
      </el-form-item>
      <el-form-item label="单位">
        <el-input v-model="newShipment.shipmentItemUnit" placeholder="请输入产品物料单位"></el-input>
      </el-form-item>
      <el-form-item label="规格型号">
        <el-input v-model="newShipment.shipmentItemSpecification" placeholder="请输入产品物料规格型号"></el-input>
      </el-form-item>
      <el-form-item label="客户名称">
        <el-input v-model="newShipment.shipmentClientName" placeholder="请输入客户名称"></el-input>
      </el-form-item>
      <el-form-item label="批次号">
        <el-input v-model="newShipment.shipmentLotNumber" placeholder="请输入批次号"></el-input>
      </el-form-item>
      <el-form-item label="发货数量">
        <el-input-number v-model="newShipment.shipmentNumber" style="width: 200px" :min ="1"></el-input-number>
      </el-form-item>
      <el-form-item label="检验数量">
        <el-input-number v-model="newShipment.materialNumber" style="width: 200px" :min ="1"></el-input-number>
      </el-form-item>
      <el-form-item label="不合格数">
        <el-input-number v-model="newShipment.shipmentOutCount" style="width: 200px" :min ="1"></el-input-number>
      </el-form-item>
      <el-form-item label="出货日期">
        <el-date-picker v-model="newShipment.shipmentDate" type="date" placeholder="请选择出货日期" style="width: 200px"></el-date-picker>
      </el-form-item>
      <el-form-item label="检测日期">
        <el-date-picker v-model="newShipment.materialDate" type="date" placeholder="请选择检测日期" style="width: 200px"></el-date-picker>
      </el-form-item>
      <el-form-item label="检测结果">
        <el-select v-model="newShipment.materialOutcome" placeholder="请选择检测结果" style="width: 200px">
          <el-option label="检测通过" :value="1"></el-option>
          <el-option label="检测不通过" :value="2"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-form label-width="120px">
      <el-form-item label="备注">
        <el-input  type="textarea" placeholder="请输入备注"></el-input>
      </el-form-item>
    </el-form>
    <el-form>
      <el-button type="primary" @click="addShipmentConfirm">确定</el-button>
      <el-button @click="addShipmentCancel">取消</el-button>
    </el-form>
  </el-dialog>
<!--  编辑弹窗-->
  <el-dialog title="修改出货检验单" v-model="editShipmentDialogVisible" width="80%" >
    <el-form :model="selectByIdShipment" :inline="true" label-width="120px">
      <el-form-item label="检验单编号">
        <el-input v-model="selectByIdShipment.shipmentId" placeholder="请输入出货检验单编号" ></el-input>
      </el-form-item>
      <el-form-item label="检验单名称">
        <el-input v-model="selectByIdShipment.shipmentName" placeholder="请输入出货检验单名称"></el-input>
      </el-form-item>
      <el-form-item label="产品编号">
        <el-input v-model="selectByIdShipment.shipmentItemId" placeholder="请输入产品物料编码"></el-input>
      </el-form-item>
      <el-form-item label="产品名称">
        <el-input v-model="selectByIdShipment.shipmentItemName" placeholder="请输入产品物料名称"></el-input>
      </el-form-item>
      <el-form-item label="单位">
        <el-input v-model="selectByIdShipment.shipmentItemUnit" placeholder="请输入产品物料单位"></el-input>
      </el-form-item>
      <el-form-item label="规格型号">
        <el-input v-model="selectByIdShipment.shipmentItemSpecification" placeholder="请输入产品物料规格型号"></el-input>
      </el-form-item>
      <el-form-item label="客户名称">
        <el-input v-model="selectByIdShipment.shipmentClientName" placeholder="请输入客户名称"></el-input>
      </el-form-item>
      <el-form-item label="批次号">
        <el-input v-model="selectByIdShipment.shipmentLotNumber" placeholder="请输入批次号"></el-input>
      </el-form-item>
      <el-form-item label="发货数量">
        <el-input-number v-model="selectByIdShipment.shipmentNumber" style="width: 200px" :min ="1"></el-input-number>
      </el-form-item>
      <el-form-item label="检验数量">
        <el-input-number v-model="selectByIdShipment.materialNumber" style="width: 200px" :min ="1"></el-input-number>
      </el-form-item>
      <el-form-item label="不合格数">
        <el-input-number v-model="selectByIdShipment.shipmentOutCount" style="width: 200px" :min ="0"></el-input-number>
      </el-form-item>
      <el-form-item label="出货日期">
        <el-date-picker v-model="selectByIdShipment.shipmentDate" type="date" placeholder="请选择出货日期" style="width: 200px"></el-date-picker>
      </el-form-item>
      <el-form-item label="检测日期">
        <el-date-picker v-model="selectByIdShipment.materialDate" type="date" placeholder="请选择检测日期" style="width: 200px"></el-date-picker>
      </el-form-item>
      <el-form-item label="检测结果">
        <el-select v-model="selectByIdShipment.materialOutcome" placeholder="请选择检测结果" style="width: 200px">
          <el-option label="检测通过" :value="1"></el-option>
          <el-option label="检测不通过" :value="2"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-form>
      <el-text>--------------------------------------------------------------------------------------检测项---------------------------------------------------------------------------------------------</el-text>
    </el-form>
    <el-form>
      <!--  内容栏-->
      <el-row style="margin-top: 15px">
        <el-col :span="24">
          <el-table stripe border style="width: 100%" :data="processTestingProjects">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column align="center" prop="testingProjectName" label="检测项名称" width="150"></el-table-column>
            <el-table-column align="center" prop="testingProjectType" label="检测项类型" width="150">
              <template #default="scope">
                <el-tag type="success" v-if="scope.row.testingProjectType == 1">尺寸</el-tag>
                <el-tag type="warning" v-else-if="scope.row.testingProjectType == 2">外观</el-tag>
                <el-tag type="danger" v-else-if="scope.row.testingProjectType == 3">重量</el-tag>
                <el-tag type="info" v-else-if="scope.row.testingProjectType == 4">性能</el-tag>
                <el-tag type="primary" v-else-if="scope.row.testingProjectType == 5">成分</el-tag>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="testingProjectTool" label="检测工具" width="150"></el-table-column>
            <el-table-column align="center" prop="testingProjectMethod" label="检测方法" width="150"></el-table-column>
            <el-table-column align="center" prop="testingProjectStandard" label="标准值" width="150"></el-table-column>
            <el-table-column align="center" prop="testingProjectUnit" label="单位" width="150"></el-table-column>
            <el-table-column align="center" prop="testingProjectErrorMax" label="误差上限" width="150"></el-table-column>
            <el-table-column align="center" prop="testingProjectErrorMin" label="误差下限" width="150"></el-table-column>
            <el-table-column align="center" prop="testingProjectFatalFlaw" label="致命缺陷数量" width="150"></el-table-column>
            <el-table-column align="center" prop="testingProjectSevereFlaw" label="严重缺陷数量" width="150"></el-table-column>
            <el-table-column align="center" prop="testingProjectSlightFlaw" label="轻微缺陷数量" width="150"></el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!--    分页栏-->
      <el-row style="margin-top: 15px">
        <el-col :span="24">
          <el-pagination @change="changePageProcessTestingProject" background layout="prev, pager, next" :total="totalTestingProject"/>
        </el-col>
      </el-row>
    </el-form>
    <el-form style="margin-top: 20px">
      <el-form-item>
        <el-button type="primary" @click="editShipmentConfirm">确定</el-button>
        <el-button @click="editShipmentCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script setup>
import {ref,onMounted} from 'vue';
import {useRouter,useRoute} from "vue-router";
import {ElMessage,ElMessageBox} from "element-plus";
import shipmentRequest from "@/api/defece/shipment.js";
import testingProjectRequest from "@/api/defece/testingProject.js";
// 封装查询对象
let shipmentQuery = ref({});
let shipments = ref([]);
let curPage = ref(1);
let total = ref(0);
// 封装新增的出货检验单信息
let newShipment = ref({});
// 新增弹窗的控制器
let newShipmentDialogVisible = ref(false);
// 封装根据id查询到的数据信息
let selectByIdShipment = ref({});
// 编辑弹窗的控制器
let editShipmentDialogVisible = ref(false);
// 封装检测项查询对象
let processTestingProjectQuery = ref({});
let processTestingProjects = ref([]);
let curPageTestingProject = ref(1);
let totalTestingProject = ref(0);
// 封装被选择的数据信息的id
let selectByIdShipmentId = ref([]);
// 获取被选中的数据信息的id
function selectShipment(queryTable){
  let listShipment = new Array();
  queryTable.forEach(item=>{
    listShipment.push(item.shipmentId);
    selectByIdShipmentId.value = listShipment;
    console.log(selectByIdShipmentId.value)
  })
}
// 批量删除的点击事件
function batchDeleteShipment(){
  for (let i=0;i<selectByIdShipmentId.value.length;i++){
    deleteShipment(selectByIdShipmentId.value[i])
  }
}
// 删除按钮的确认事件
function deleteShipment(shipmentId){
  shipmentRequest.loadDeleteShipment(shipmentId,(res)=>{
    if (res.code == 200){
      ElMessage({
        message: "删除成功",
        type: "success"
      })
    }
    loadShipments();
  })

}
// 加载检测项分页栏
function changePageProcessTestingProject(value){
  curPageTestingProject.value = value;
  loadTestingProjectProcess();
}
// 加载检测项列表
function loadTestingProjectProcess(){
  testingProjectRequest.loadTestingProject(processTestingProjectQuery.value,curPageTestingProject.value,10,res=>{
    totalTestingProject.value = res.data.total;
    processTestingProjects.value = res.data.list;
    console.log(processTestingProjects.value)
  })
}
// 编辑弹窗的确认按钮的点击事件
function editShipmentConfirm(){
  shipmentRequest.loadUpdateShipment(selectByIdShipment.value,(res)=>{
    if (res.code==200){
      ElMessage({
        message: "修改成功",
        type: "success"
      })
    }
    editShipmentDialogVisible.value = false;
    loadShipments();
  })
}
// 编辑弹窗的取消按钮的点击事件
function editShipmentCancel(){
  editShipmentDialogVisible.value = false;
  selectByIdShipment.value = {};
}
// 编辑按钮的点击事件
function editShipment(shipmentId){
  shipmentRequest.loadSelectByIdShipment(shipmentId,(res)=>{
    selectByIdShipment.value = res.data;
    editShipmentDialogVisible.value = true;
    console.log(selectByIdShipment.value)
  })
  loadTestingProjectProcess();
}
// 新增按钮的点击事件
function addShipment(){
  newShipmentDialogVisible.value = true;
}
// 新增弹窗的确认按钮的点击事件
function addShipmentConfirm(){
  shipmentRequest.loadAddShipment(newShipment.value,(res)=>{
    ElMessage.success(res.message);
    newShipmentDialogVisible.value = false;
    loadShipments();
  })
}
// 新增弹窗的取消按钮的点击事件
function addShipmentCancel(){
  newShipmentDialogVisible.value = false;
  newShipment.value = {};
}
// 加载项目列表
function loadShipments(){
  shipmentRequest.loadShipment(shipmentQuery.value,curPage.value,10,(res)=>{
    total.value = res.data.total;
    shipments.value = res.data.list;
    console.log(shipments.value)
})
}
// 分页栏事件
function changePage(value){
  curPage.value = value;
  loadShipments();
}
onMounted(()=>{
  loadShipments();
})
</script>
<style scoped>

</style>